package wili.b1.transform;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;

import wili.attrMapping._user.b1.AttConst;
import wili.attrMapping._user.b1.AttMapingRule_main;
import wili.attrMapping._user.b1.B1com;
import wili.attrMapping.core.AttsFile;
import wili.attrMapping.file.JsonFile;
import wili.c.Ca;
import wili.json.JsonLeaf;
import wili.json.JsonNode;

/**
 *
 */
public class JsonNode9 implements AttConst {
	public static void moveJsonNodeList(File from, String att, File to) {
		JsonFile to2 = new JsonFile(to);
		// moveAttToFile(from, att, to2);
		Ca.asert(false, "todo");
	}

	/**
	 * 把文件中所有的att移动到toJson中。移动规则
	 * 
	 * @param fromFile
	 * @param toFile
	 * @param att
	 * @param value
	 */
	public static void moveAttToFile(File fromFile, String att, StringBuilder msgBuf, Map<String, JsonFile> id2fileMap,
			Map<String, List<AttsFile>> keyToFilesMap__) {
		// 找到属性，找到id, 找到文件，文件。
		// find att, find att.id, find target file, updat file.
		JsonFile json1 = new JsonFile(fromFile);
		@SuppressWarnings("all")
		List<JsonNode> fromLeafs = (List) json1.findNodesByName(att);
		BiPredicate<JsonNode, JsonFile> cons = new BiPredicate<JsonNode, JsonFile>() {
			@Override
			public boolean test(JsonNode fromLeaf, JsonFile toJsonFile___) {
				String guid = fromLeaf.dad.toObj().guid;
				// Ca.log("guid=" + guid);
				if (guid.contains("589c6f58-fb44-45cf-8731-7d4eb9388b2c"))
					Ca.pause();
				JsonFile toFile = id2fileMap.get(guid);
				if (toFile == null)
					return false;
				if (false && !toFile.file.getName().equals("accountingTab.fragment.json"))
					return false;
				JsonNode toObj = toFile.findObj_byAtt("guid", guid);
				if (toObj == null) {
					Ca.log(" guid not found: " + guid);
					return false;
				}
				String fname = toFile.file.getName();
				if (B1com.dadDad_is_content(fromLeaf))
					return false;
				String fromLeafSt = fromLeaf.stringValue;// .getString(false);
				JsonNode toAtt = toObj.son(att);
				if (toAtt != null) {
					String toSt;
					if (toAtt instanceof JsonLeaf) {
						toSt = toAtt.stringValue;
					} else {
						toSt = toAtt.getString(false);
					}
					fromLeafSt = B1com.quoteNum_relatesToObjType(fromLeafSt);
					if (!fromLeafSt.replaceAll("\\s+", "").equals(toSt.replaceAll("\\s+", ""))) {
						{
							++updateTimes;
							String st = "\n\n" + updateTimes + "," + fname + ", guid: " + guid;
							msgBuf.append(st);
						}
						msgBuf.append("\n " + Bugs.msg(Bugs.afterUpdate) + fromLeafSt);
						fromLeaf.setStringValue(fromLeafSt);
						msgBuf.append("\n " + Bugs.msg(Bugs.beforeUpdate) + toSt);
						if (toSt.startsWith("="))
							toSt = toAtt.getString(false);
						toAtt.replaceBy(fromLeaf);
						AttMapingRule_main.bugNum++;
					}
				} else {
					{
						++updateTimes;
						String st = "\n\n" + updateTimes + "," + fname + ", guid: " + guid;
						msgBuf.append(st);
					}
					fromLeafSt = B1com.quoteNum_relatesToObjType(fromLeafSt);
					msgBuf.append("\n " + Bugs.msg(Bugs.afterUpdate) + fromLeafSt);
					msgBuf.append("\n " + Bugs.msg(Bugs.beforeUpdate) + "无.");
					toObj.addSon(fromLeaf);
				}
				toFile.updateTimes[0]++;
				return true;
			}
		};
		moveJsonNodeList(fromLeafs, cons);
	}

	/**
	 * 把文件中所有的att移动到toJson中。移动规则
	 * 
	 * @param fromFile
	 * @param toFile
	 * @param att
	 * @param value
	 */
	public static void moveAttToFileNew(File fromFile, String att, StringBuilder msgBuf,
			Map<String, List<AttsFile>> attsIdToFilesMap) {
		// 找到属性，找到id, 找到文件，文件。
		// find att, find att.id, find target file, updat file.
		Ca.asert(false, "11-11");
		JsonFile json1 = new JsonFile(fromFile);
		@SuppressWarnings("all")
		List<JsonNode> fromLeafs = (List) json1.findNodesByName(att);
		BiPredicate<JsonNode, JsonFile> cons = new BiPredicate<JsonNode, JsonFile>() {
			@Override
			public boolean test(JsonNode fromLeaf, JsonFile toJsonFile___) {
				String guid = fromLeaf.dad.toObj().guid;
				// Ca.log("guid=" + guid);
				if (guid.contains("bfdffbcd-4cf1-4e47-9b12-57871d5f855a"))
					Ca.pause();
				List<AttsFile> ff = attsIdToFilesMap.get(guid);
				JsonFile toFile = ff == null ? null : (JsonFile) ff.get(0);
				if (toFile == null)
					return false;
				JsonNode toObj = toFile.findObj_byAtt("guid", guid);
				if (toObj == null) {
					Ca.log(" guid not found: " + guid);
					return false;
				}
				String fname = toFile.file.getName();
				if (B1com.dadDad_is_content(fromLeaf))
					return false;
				String fromLeafSt = fromLeaf.stringValue;// .getString(false);
				JsonNode toAtt = toObj.son(att);
				if (toAtt != null) {
					String toSt;
					if (toAtt instanceof JsonLeaf) {
						toSt = toAtt.stringValue;
					} else {
						toSt = toAtt.getString(false);
					}
					fromLeafSt = B1com.quoteNum_relatesToObjType(fromLeafSt);
					if (!fromLeafSt.replaceAll("\\s+", "").equals(toSt.replaceAll("\\s+", ""))) {
						{
							++updateTimes;
							String st = "\n\n" + updateTimes + "," + fname + ", guid: " + guid;
							msgBuf.append(st);
						}
						msgBuf.append("\n " + Bugs.msg(Bugs.afterUpdate) + fromLeafSt);
						fromLeaf.setStringValue(fromLeafSt);
						msgBuf.append("\n " + Bugs.msg(Bugs.beforeUpdate) + toSt);
						if (toSt.startsWith("="))
							toSt = toAtt.getString(false);
						toAtt.replaceBy(fromLeaf);
						AttMapingRule_main.bugNum++;
					}
				} else {
					{
						++updateTimes;
						String st = "\n\n" + updateTimes + "," + fname + ", guid: " + guid;
						msgBuf.append(st);
					}
					fromLeafSt = B1com.quoteNum_relatesToObjType(fromLeafSt);
					msgBuf.append("\n " + Bugs.msg(Bugs.afterUpdate) + fromLeafSt);
					msgBuf.append("\n " + Bugs.msg(Bugs.beforeUpdate) + "无.");
					toObj.addSon(fromLeaf);
				}
				toFile.updateTimes[0]++;
				return true;
			}
		};
		moveJsonNodeList(fromLeafs, cons);
	}

	/**
	 * 把 list 中所有的结点移动到toJson中, 目标位置由 pred指定。
	 * 
	 * @param fromLeafs
	 * @param toJson
	 * @param pred
	 */
	public static void moveJsonNodeList(List<JsonNode> fromLeafs, BiPredicate<JsonNode, JsonFile> pred) {
		int times = 0;
		for (int i = 0; i < fromLeafs.size(); i++) {
			JsonNode son = fromLeafs.get(i);
			if (pred.test(son, null))
				++times;
		}
		if (times > 0) {
			// toJson.saveToFile(null);
		}
	}

	static int inc, updateTimes;

	public static void main(String[] args) {
		File fDir = new File(
				"C:\\Perforce\\c1816431\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\node\\client\\src\\content\\sales\\boODOC\\f");
		File tDir = new File(
				"C:\\Perforce\\c1816431\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\node\\client\\src\\content\\sales\\boODOC\\fragment\\a");
		File[] ff = { new File(fDir, "SalesDoc.detailView_acc.json"), new File(tDir, "accountingTab.fragment.json"), //
				new File(fDir, "acc_819c2c615aar.json"), new File(tDir, "accountingTab_AR.fragment.json"), //
				new File(fDir, "acc_819c2c615abr.json"), new File(tDir, "accountingTab_BR.fragment.json") };
		for (int i = 4; i < ff.length; i += 2) {
			moveJsonNodeList(ff[i], "change", ff[i + 1]);
			break;
		}
	}
}
